package edu.hgnc.aopTest;


import edu.hgnc.aop.annotation.config.SpringConfig;
// import edu.hgnc.aop.xml.service.MyCalculator;
import edu.hgnc.aop.annotation.service.MyCalculator1;
import edu.hgnc.aop.xml.service.MyCalculator;
import edu.hgnc.cglib.MyCglib;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;
import org.springframework.cglib.core.DebuggingClassWriter;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.lang.reflect.Field;
import java.util.Properties;

/**
 * 测试循环依赖
 */
public class JustTest2 {

	private static final Log logger = LogFactory.getLog(JustTest2.class);

	@Test
	public void test01() {
		logger.info("======================================================");
		logger.info("hello,when you see me,that's mean you are test succeed~");
		logger.info("======================================================");
	}

	@Test
	public void test02() {
		if (logger.isDebugEnabled()){
			logger.info("开始执行");
			// 动态代理创建的class文件存储到本地
			System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY,"C:\\Users\\admin\\Desktop\\testCglib");
			//通过cglib动态代理获取代理对象的过程，创建调用的对象
			Enhancer enhancer = new Enhancer();
			// 设置enhancer对象的父类
			enhancer.setSuperclass(edu.hgnc.cglib.MyCalculator.class);
			// 设置enhance的回调对象
			enhancer.setCallback(new MyCglib());
			// 创建代理对象
			MyCalculator1 myCalculator = (MyCalculator1) enhancer.create();

			myCalculator.add(1,1);
			logger.debug(myCalculator.getClass());
		}
	}

	// 使用xml方式导入
	@Test
	public void test03() {
		logger.info("开始执行");
		if (logger.isDebugEnabled()){
			// saveGeneratedCGlibProxyFiles(System.getProperty("user.dir")+"/proxy");

			ApplicationContext context = new ClassPathXmlApplicationContext("testAop2.xml");
			MyCalculator bean = context.getBean(MyCalculator.class);
			int i = bean.add(1, 1);

			logger.debug(i);
		}
	}

	// 使用注解方式
	@Test
	public void test04() throws Exception{
		logger.info("开始执行");
		if (logger.isDebugEnabled()){
			saveGeneratedCGlibProxyFiles(System.getProperty("user.dir")+"/proxy");

			logger.debug("----------------------------------------------------------------------");
			AnnotationConfigApplicationContext context2 = new AnnotationConfigApplicationContext();
			context2.register(SpringConfig.class);
			context2.refresh();
			MyCalculator1 bean2 = context2.getBean(MyCalculator1.class);
			int i2 = bean2.add(1, 3);
			logger.debug(i2);
		}

	}

	public static void saveGeneratedCGlibProxyFiles(String s) throws Exception{
		Field field = System.class.getDeclaredField("props");
		field.setAccessible(true);
		Properties props = (Properties) field.get(null);
		System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY,s); // 保存目录
		props.put("net.sf.cglib.core.DebuggingClassWriter.traceEnabled","true");
	}

}
